import { actionTypes } from '../actions/todo';

//定义默认状态
const initState = {
    display: 'all', //显示状态(all,uncompleted,completed)
    todos: []
};

const reducer = (state = initState, action) => {
    let newState;
    switch (action.type) {
        case actionTypes.ADD_TODO:
            newState = {
                todos: [...state.todos, action.payload]
            };
            break;
        case actionTypes.TOGGLE_COMPLETE:
            newState = {
                todos: state.todos.map(item => {
                    if (item.id == action.payload) {
                        item.isComplete = !item.isComplete;
                    }
                    return item;
                })
            };
            break;
        case actionTypes.DEL_TODO:
            newState = {
                todos: state.todos.filter(todo => todo.id != action.payload)
            };
            break;
        case actionTypes.CHANGE_DISPLAY:
            console.log('是');
            console.log(action.payload);
            newState = {
                display: action.payload,
                todos: [...state.todos]
            };
            break;
        case actionTypes.ADD_TODO_ASYNC:
            newState = {
                todos: [...state.todos, action.payload]
            };
            break;
        default:
            newState = state;
            break;
    }
    return newState;
};
export default reducer;
